package com.example.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.servicebase.client.EduClient;
import com.example.servicebase.client.UcenterClient;
import com.example.servicebase.exceptionhandler.CustomException;
import com.example.statistics.entity.Daily;
import com.example.statistics.mapper.DailyMapper;
import com.example.statistics.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-06-07
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Qualifier("com.example.servicebase.client.UcenterClient")
    @Autowired
    private UcenterClient userClient;

    @Autowired
    private EduClient eduClient;

    //统计某一天的信息
    @Override
    public void registerCount(String date) {
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Date now=new Date();
        Date To = null;
        try {
            To = dateFormat1.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        if (To.after(now)){
            throw new CustomException(20001,"统计日期不得超过今天！");
        }
        //查询数据库当天是否有数据
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", date);
        Daily daily1 = baseMapper.selectOne(wrapper);

        //远程调用获取注册人数
        Integer registerCount = userClient.countRegister(date);
        //获取登录人数
        Integer loginCount = userClient.LoginCount();
        //获取浏览数
        Integer viewCount = eduClient.getViewCount();
        //获取新增课程数
        Integer courseCount = eduClient.CourseCount();
        //创建统计对象
        Daily daily = new Daily();
        daily.setRegisterNum(registerCount);
        //TODO 待完成，统计当前日期的登录人数，播放视频数，新增课程数
        daily.setLoginNum(loginCount);
        daily.setVideoViewNum(viewCount);
        daily.setCourseNum(courseCount);
        daily.setDateCalculated(date);
        if (daily1 == null) {
            //表中没有当前日期，做插入
            baseMapper.insert(daily);
        } else {
            //做更新
            daily1.setRegisterNum(registerCount);
            daily.setLoginNum(loginCount);
            daily.setVideoViewNum(viewCount);
            daily.setCourseNum(courseCount);
            baseMapper.updateById(daily1);
        }
    }
    //返回用于图表显示的数据
    @Override
    public Map<String, Object> getShowData(String type, String start, String end) {
        //根据条件进行查询
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",start,end)
                .select("date_calculated",type);
        List<Daily> dailyList = baseMapper.selectList(wrapper);
        //将数据进行封装
        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for (Daily daily : dailyList) {
            dateList.add(daily.getDateCalculated());
            switch (type){
                case "register_num":numList.add(daily.getRegisterNum());break;
                case "login_num":numList.add(daily.getLoginNum());break;
                case "video_view_num":numList.add(daily.getVideoViewNum());break;
                case "course_num":numList.add(daily.getCourseNum());break;
                default:break;
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numList",numList);
        return map;
    }
}
